/**
 * fileName:  QueryImpl.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-3-4 下午9:40:35
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-3-4          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.udap.database.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.cloudland.udap.database.Deserialize;
import org.cloudland.udap.database.Query;
import org.cloudland.udap.database.Result;
import org.cloudland.udap.database.entity.ManagerEntity;
import org.cloudland.udap.database.entity.MenuEntity;
import org.cloudland.udap.database.entity.RoleEntity;
import org.cloudland.udap.database.support.ibatis.dese.OueryManagerMapper;
import org.cloudland.udap.database.terms.Terms;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  QueryImpl
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class QueryImpl<L extends Deserialize<L>> implements Query<L> {
	
	private SqlSession session = null;
	
	private ArrayList<Terms> array = new ArrayList<Terms>(10);
	
	private Deserialize<L> mapper;
	
	public QueryImpl(SqlSession session) {
		this.session = session;
	}
	
	/**
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Query<L> find(Class<L> clazz) {
		
		if (ManagerEntity.class.isAssignableFrom(clazz)) {
			mapper = (Deserialize<L>) this.session.getMapper(OueryManagerMapper.class);
		} else if (MenuEntity.class.isAssignableFrom(clazz)) {
			//mapper = this.session.getMapper(MenuMapper.class);
		} else if (RoleEntity.class.isAssignableFrom(clazz)) {
			//mapper = this.session.getMapper(RoleMapper.class);
		}
		
		return this;
	}
	
	/**
	 * @param terms
	 * @return
	 */
	@Override
	public Query<L> add(Terms terms) {
		array.add(terms);
		return this;
	}
	
	/**
	 * @return
	 */
	@Override
	public Query<L> orderBy() {
		return null;
	}
	
	/**
	 * @return
	 */
	@Override
	public Query<L> limit() {
		return null;
	}
	
	/**
	 * @param begin
	 * @return
	 */
	@Override
	public Query<L> limit(Integer begin) {
		return null;
	}
	
	/**
	 * @param begin
	 * @param size
	 * @return
	 */
	@Override
	public Query<L> limit(Integer begin, Integer size) {
		return null;
	}
	
	/**
	 * @return
	 */
	@Override
	public Result<L> result() {
		
		Map<String, String> params = new HashMap<String, String>();
		for (Terms terms : array) {
			params.put(terms.key(), terms.value());
		}
		
		List<L> list = mapper.find(params);
		if (null == list || list.size() <= 0) {
			list = new ArrayList<L>(0);
		}
		
		return new ResultImpl<L>(list);
	}

}
